package com.lhh.jiu.controller;

import com.lhh.jiu.data.Result;
import com.lhh.jiu.entity.Test1;
import com.lhh.jiu.service.Test1Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Api("测试类1")
@Controller
public class Test1Controller {
    @Autowired
    Test1Service test1Service;

    @ApiOperation("查询所有的信息，不分页")
    @GetMapping("/test1")
    @ResponseBody
    public Object getAll(){
        List<Test1> list = test1Service.getAll();
        return Result.getHashMapList(list);
    }

    //添加一个数据
    @ApiOperation("添加或者更新一个数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "编号",paramType = "query"),
            @ApiImplicitParam(name="name",value = "供应商",paramType = "query"),
            @ApiImplicitParam(name="content",value = "描述",paramType = "query"),
    })
    @PostMapping("/test1")
    @ResponseBody
    public Object adminAdd(@RequestParam(value = "id",required = false)Integer id
            ,@RequestParam(value = "name",required = false)String name
            ,@RequestParam(value = "content",required = false)String content){
        Test1 test1 = new Test1();
        test1.setId(id);
        test1.setName(name);
        test1.setContent(content);
        test1.setCreatetime(new Date());
        try {
            //添加数据，会返回一个数据，查看是否插入成功
            test1Service.insertOrUpdate(test1);
            return Result.getHashMapSave(1);
        }catch (Exception e){
            return Result.getHashMapException(e);
        }
    }
}
